package com.spring.sunflower.common;

import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.spring.sunflower.R;
import com.spring.sunflower.base.BaseActivity;
import com.spring.sunflower.constant.Constant;
import com.spring.sunflower.dialog.OnPermissionIntroClickListener;
import com.spring.sunflower.util.AppUtils;

/**
 * Created by Kevin on 2021/6/30<br/>
 * <p>
 * Describe:<br/>
 */
public class BindMobileActivity extends BaseActivity<BindPhonePresenter> implements CommonContract.BindPhoneView, View.OnClickListener {
    private EditText etPhoneNum, etCode;
    private TextView tvDone, tvGetCode;
    private CountDownTimer countDownTimer;
    private boolean isOnTick = false;

    @Override
    public void initView() {
        tvTitle.setText("绑定手机号");
        etPhoneNum = findViewById(R.id.etPhone);
        etCode = findViewById(R.id.etPhoneCode);
        tvDone = findViewById(R.id.tvDone);
        tvGetCode = findViewById(R.id.tvGetCode);
        etPhoneNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (etPhoneNum.getText().toString().trim().length() == 11) {
                    tvGetCode.setTextColor(getColorX(R.color.colorPrimary));
                } else {
                    if (isOnTick) return;
                    tvGetCode.setTextColor(getColorX(R.color.gray_4));
                }
            }
        });
        tvGetCode.setOnClickListener(this);
        tvDone.setOnClickListener(this);

    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_bind_phone;
    }

    @Override
    public BindPhonePresenter setPresenter() {
        return new BindPhonePresenter(this);
    }


    @Override
    public void bindSuccess() {
        String mPhoneNum = etPhoneNum.getText().toString().trim();
        saveField(Constant.KEY_MOBILE, mPhoneNum);
        showToast("绑定成功");
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvGetCode:
                if (isOnTick) return;
                String mPhoneNum = etPhoneNum.getText().toString().trim();
                if (TextUtils.isEmpty(mPhoneNum)) {
                    showToast("手机号不能为空");
                    return;
                }
                if (mPhoneNum.length() != 11 || !AppUtils.isValidPhoneNum(mPhoneNum)) {
                    showToast("请输入正确手机号");
                } else {
                    mPresenter.requestAuthCode(mPhoneNum, "0");
                    startCountDown();
                }
                break;
            case R.id.tvDone:
                String code = etCode.getText().toString().trim();
                String phoneNum = etPhoneNum.getText().toString().trim();
                if (TextUtils.isEmpty(phoneNum)) {
                    showToast("手机号不能为空");
                    return;
                }
                if (phoneNum.length() != 11 || !AppUtils.isValidPhoneNum(phoneNum)) {
                    showToast("请输入正确手机号");
                    return;
                }

                if (TextUtils.isEmpty(code)) {
                    showToast("验证码不能为空");
                    return;
                }
                if (code.length() != 4) {
                    showToast("验证码位数不够");
                    return;
                }
                showNoticeDialog("温馨提示", "绑定手机号提示", "请检查你要绑定的手机号，并确认是否要绑定该手机号", "确定", new OnPermissionIntroClickListener() {
                    @Override
                    public void onClickCancel() {

                    }

                    @Override
                    public void onClickOK() {
                        mPresenter.bindPhone(phoneNum, code);
                    }
                });
        }
    }

    public void startCountDown() {
        isOnTick = true;
        countDownTimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tvGetCode.setText("倒计时" + millisUntilFinished / 1000 + "s");
            }

            @Override
            public void onFinish() {
                isOnTick = false;
                tvGetCode.setText("重新获取");
            }
        };
        countDownTimer.start();
    }
}
